package com.nnnu.wsnackshop.controller.admin;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.AddTagDTO;
import com.nnnu.wsnackshop.pojo.dto.TagQueryDTO;
import com.nnnu.wsnackshop.pojo.dto.UpdateTagDTO;
import com.nnnu.wsnackshop.pojo.vo.TagVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.ITagsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 商品标签 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("adminTagsController")
@RequestMapping("/api/admin/tags")
@Slf4j
@Validated
@Tag(name = "管理端-标签接口")
@RequiredArgsConstructor
public class TagsController {

    private final ITagsService tagsService;

    @Operation(summary = "分页查询标签")
    @GetMapping("/page")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("tag:read")
    public Result<?> pageTags(@Valid @ParameterObject TagQueryDTO dto) {
        log.info("分页查询标签：{}", dto);
        return Result.success(tagsService.pageTags(dto), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "新增标签")
    @PostMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("tag:create")
    public Result<?> addTag(@Valid @RequestBody @Parameter(description = "新增标签DTO") AddTagDTO dto) {
        log.info("新增标签：{}", dto);
        tagsService.addTag(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "更新标签")
    @PutMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("tag:update")
    public Result<?> updateTag(@Valid @RequestBody @Parameter(description = "更新标签DTO") UpdateTagDTO dto) {
        log.info("更新标签：{}", dto);
        tagsService.updateTag(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "删除标签")
    @DeleteMapping("/delete")
    @RequiresRoles("superAdmin")
    @RequiresPermissions("tag:delete")
    public Result<?> batchDelete(@RequestParam @Parameter(description = "标签ID") List<Integer> ids) {
        log.info("删除标签：{}", ids);
        tagsService.deleteTags(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "获取标签详情")
    @GetMapping("/{id}")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("tag:read")
    public Result<TagVO> getDetail(@PathVariable @Parameter(description = "标签ID") Long id) {
        log.info("查询标签详情：{}", id);
        return Result.success(tagsService.getDetail(id), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "修改标签状态")
    @PutMapping("/{id}/status/{status}")
    @RequiresRoles("superAdmin")
    @RequiresPermissions("tag:update")
    public Result<Void> updateStatus(@PathVariable @Parameter(description = "标签ID", in = ParameterIn.PATH) Long id,
                                     @PathVariable @Parameter(description = "状态 0禁用 1启用", in = ParameterIn.PATH) Integer status) {
        log.info("修改标签状态：id={}, status={}", id, status);
        tagsService.updateStatus(id, status);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }


    @Operation(summary = "导出标签Excel", description = "仅 superAdmin 且拥有 tag:export 权限可访问")
    @GetMapping("/export")
    @RequiresRoles("superAdmin")
    @RequiresPermissions("tag:export")
    public void exportTags(HttpServletResponse response) throws IOException {
        log.info("导出标签Excel");
        tagsService.exportTags(response);
    }

    @Operation(summary = "导入标签Excel", description = "仅 superAdmin 且拥有 tag:import 权限可访问")
    @PostMapping("/import")
    @RequiresRoles("superAdmin")
    @RequiresPermissions("tag:import")
    public Result<Void> importTags(@RequestParam("file") MultipartFile file) throws IOException {
        log.info("导入标签Excel");
        tagsService.importTags(file);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "下载标签导入模板")
    @GetMapping("/template")
    @RequiresRoles("superAdmin")
    @RequiresPermissions("tag:read")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        log.info("下载标签导入模板");
        tagsService.downloadTemplate(response);
    }
}
